#include<vector>
#include<algorithm>
using namespace std;
class Solution {
private:
    int minDomino(int x, vector<int>& A, vector<int>& B) {
        int rotate_a = 0;
        int rotate_b = 0;
        for (int i = 0; i < A.size(); ++i) {
            if (A[i] != x && B[i] != x) return -1;
            if (A[i] != x) ++rotate_b;
            if (B[i] != x)++rotate_a;
        }
        return min(rotate_b, rotate_a);
    }

public:
    int minDominoRotations(vector<int>& A, vector<int>& B) {
        int res;
        res = minDomino(A[0], A, B);
        if (res !=-1||A[0] == B[0]) return res;
        res = minDomino(B[0], A, B);
        return res;
    }
};